/*
 * 1.初始化评论星
 * 2.评论星动画
 * 3.评论输入框的字数限制及剩余字数提示
 * 4.投诉理由输入框的字数限制及剩余字数提示
 * 5.提交评论 rate-btn 都是异步
 * 6.星星评分提交 kartik
 * 7.提交投诉理由 comp-btn
 * 
 */
(function(){
	var obj={};
	
	obj.ajax=function(type,url,data,headers,context,callback){
		$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
		.done(callback);
	};
	window.j=obj;
})();

function CommentComponent(template){
	this.model=null;
	
	this.template=template;

	//提交评论及评分
	this.submitCmt=function(cmtinfo){
		j.ajax("POST",
				"cmt",
				cmtinfo,
				{"Content-Type":"application/json"},
				this,
				function(ordercmt){
					this.model=ordercmt;
					cmt_render(this.model);
				});
	}
	
	//提交投诉
	this.submitComp=function(compobj){
		j.ajax("POST", 
				"comp", 
				compobj, 
				{"Content-Type":"application/json"}, 
				this, 
				function(){
					$("#myModal").modal("show");
			    	$(this).attr("disabled","disabled");
				});
	}
	
	
	function cmt_render(data){
		template.empty();
		var outter1=$("<div></div>").addClass("ordercomment");
		var outter2=$("<div></div>").addClass("panel panel-success");
		var outter31=$("<div></div>").addClass("panel-heading");
		var outter32=$("<div></div>").addClass("panel-body");
		var ul=$("<ul></ul>").addClass("list-group");
		var cmtli=$("<li></li>").addClass("list-group-item").attr("id","cmt");
		var timespan=$("<span></span>").addClass("cmt-time").text(data.ctime);
		var p1=$("<p></p>").text("您在"+timespan+"发表了评论：");
		alert(data.comments);
		var pcmt=$("<p></p>").addClass("publish").text(data.comments);
		
		cmtli.append(p1);
		cmtli.append(pcmt);
		ul.append(cmtli);
		outter32.append(ul);
		outter2.append(outter31);
		outter2.append(outter32);
		outter1.append(outter2);
		outter1.appendTo(template);
		
	}
}

$(function(){
	alert("comein");
	var o=new CommentComponent($(".ordercomment"));
	
	//评价星初始化
    $("#rate-submit").on("click",function(){
        var rate=$("#kartik").val();
        alert(rate);
    });

    
    //评论输入框字数限制及剩余字数提示
    $("#cmttext").keyup(function(){
        //alert("hello");
        var len=$(this).val().length;
        if(len>49){
            $(this).val($(this).val().substring(0,50));
        }
        var num=50-len;
        $("#word").text(num);
    });
    
    // 投诉输入框字数限制及剩余字数提示 
    $("#comptext").keyup(function(){
        //alert("hello");
        var len=$(this).val().length;
        if(len>99){
            $(this).val($(this).val().substring(0,100));
        }
        var num=100-len;
        $("#compword").text(num);
    });
	
    $("#rate-btn").on("click",function(e){
    	alert("come");
    	var grade=$("input[name=grade]","#star-rating").val();
    	alert(grade);
    	var comment=$("#cmttext").val();
    	alert(comment);
    	var oid=$("input[name=oid]",".fillcmt").val();
    	alert(oid);
    	var rate={"oid":oid,"comments":comment,"oclevel":grade};
    	o.submitCmt(rate);
    });
	
    $("#submitcomp").on("click",function(e){
    	var complaint=$("#comptext").text();
    	$("#myModal").modal("hide");
    	//模态框消失后，投诉按钮禁用
    	$("#compbtn").attr("disabled",disabled);
    });
    
    $("#compbtn").on("click",function(e){
    	var oid=$("input[name=oid]","#complaint-button").val();
    	var complaint=$("#comptext").val();
    	var compobj={};
    	o.submitComp(compobj);
    	
    });
	
})



